/*
 * Copyright 2020-2025 the original author or authors.
 * You cannot use this file unless authorized by the author.
 */
package org.ipig.commons.helper;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.pool.KryoPool;
import org.objenesis.strategy.StdInstantiatorStrategy;

/**
 * 请在此处添加注释
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id: KryoHelper.java 3 2020-01-21 09:36:33Z ts <comchnts@163.com> $$
 * @since 1.0
 */
public class KryoHelper {
    //    private static final KryoHelper kryo=new KryoHelper();
    private static volatile KryoPool kryoPool = null;

    private KryoHelper() {
    }

    public static KryoPool getKryoPool() {
/*        KryoFactory factory = new KryoFactory() {
            public Kryo create() {
                return new Kryo();
            }
        };
        kryoPool = (new KryoPool.Builder(factory)).softReferences().build();*/
        if (kryoPool == null) {
            synchronized (KryoHelper.class) {
                kryoPool = new KryoPool.Builder(() -> {
                    final Kryo kryo = new Kryo();
                    kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
                    return kryo;
                }).softReferences().build();
            }
        }
        return kryoPool;
    }

/*    public byte[] desc(Object obj){
        KryoPool kryoPool= KryoHelper.getKryoPool();
        byte[] bytes = (byte[]) kryoPool.run(new KryoCallback<byte[]>() {
            public byte[] execute(Kryo kryo) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                Output output = new Output(baos);
                kryo.writeClassAndObject(output, merge);
                output.close();
                return baos.toByteArray();
            }
        });
    }*/

}
